﻿using Auth0;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Security.Claims;
using System.Security.Principal;
using System.Web;
using System.Web.Security;
using WorkBasket.Data.Entities;
using WorkBasket.Data.Repository;
using WorkBasket.Models;

namespace WorkBasket.Infrastructure
{
    public static class AuthFunctions
    {
       private static Auth0.Client client = new Auth0.Client(
                                ConfigurationManager.AppSettings["auth0:ClientId"],
                                ConfigurationManager.AppSettings["auth0:ClientSecret"],
                                ConfigurationManager.AppSettings["auth0:Domain"]);

       public static bool ValidateEnterpriseConnection(string ConnectionName)
       {
           Auth0.Page<Auth0.Connection> Connections = client.GetEnterpriseConnections();

           if (Connections.Where(c => c.Name.ToLower() == ConnectionName.ToLower()).Count() > 0)
               return true;
           else
               return false;
 
     
       }
       public  static Auth0.Page<Auth0.User> SocialUsersSearch(string search)
       {
            Auth0.Page<Auth0.User> Users = client.GetSocialUsers(search);
            return Users;

       }
       public static Auth0.Page<Auth0.User> EnterpriseUsersSearch(string search, string Connection)
       {
           Auth0.Page<Auth0.User> Users = client.GetUsersByConnection(Connection,search);
           return Users;

       }
       public static Auth0.UserProfile GetUserByID(string UserID)
       {
           //you might eventuall change this to be local since your going to farm logins and add meta data outside of Auth0

           return client.GetUser(UserID);

       }
       public static Auth0.UserProfile CurrentUser()
       {
          return GetUserByID( ClaimsPrincipal.Current.FindFirst("user_id").Value);

       }
       public static string CurrentUserID()
       {
           return ClaimsPrincipal.Current.FindFirst("user_id").Value;

       }


       public static IQueryable<WBUser> DBUserSearch(string email, string name)
       {
           SOC soc = new SOC(new MyContext());
           return soc.WBUsers.Where(c => c.email == email || c.name == name);
       }

       public static UserProfile CreateUser(string Email, string Password)
       {
           return client.CreateUser(Email, Password, "Username-Password-Authentication", true);
 
       }
    }
}